﻿using System;
using System.Windows.Forms;
using Microsoft.Practices.Unity;

using SharedGenomics.Core.Configuration;
using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Workbench.Views.WindowsForms;

namespace ROWorkbench
{
    public class WindowsFormsViewContainerConfigurationStrategy : IContainerConfigurationStrategy
    {
        public void Configure(ContainerConfigurationContext context)
        {
            var container = context.Container;

            container.RegisterType<IViewFactory, WindowsFormsViewFactory>();

            //register views with the container
            container.RegisterType<ILoginView, SharedGenomics.Workbench.Views.WindowsForms.LoginForm>();
            container.RegisterType<IMenuFactory, SharedGenomics.Workbench.Views.WindowsForms.MenuFactory>();
            container.RegisterType<IMenuBarMenuFactory, SharedGenomics.Workbench.Views.WindowsForms.MenuBarMenuFactory>();
            container.RegisterType<ICursorManager, WindowsFormsCursorManager>();

            //register main form with the view manager
            IViewManager<Control> viewManager = container.Resolve<IViewManager<Control>>();
            container.RegisterType<CategoryFilterViewFactory, SharedGenomics.Workbench.Views.WindowsForms.WindowsFormsCategoryFilterViewFactory>();
            container.RegisterType<CategoryValueViewFactory, SharedGenomics.Workbench.Views.WindowsForms.WindowsCategoryValueViewFactory>();

            /*NOTE: the following block sets up all the view-related container mappings (except the WindowsFormsViewManager which is slightly more complicated as it is a singleton) using
             * everyone's favourite programming technique: reflection. The view assembly is loaded and the required types are registered with the container - this means that the main 
             * project does not need a static dependency on the view assembly at all which means that the ROWorkbench project could be removed and the SharedGenomics.Workbench project 
             * could be made the main project. It would also make things easier if another set of views (e.g. WPF...) were added since only the assembly to load would need to be changed. 
             * At the moment this is not used since the current explicit mechanism is easier to understand and more reliable in the event of multiple possible mappings in the view assembly...
             */
            //Assembly viewAssembly = Assembly.Load("SharedGenomics.Workbench.Views.WindowsForms");
            //DynamicTypeMapper viewTypeMapper = new DynamicTypeMapper(c);
            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<IViewManager<Control>>>();
            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<IViewFactory>>();
            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<IViewDisplayer>>();
            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<ICursorManager>>();
            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<IMenuFactory>>();
            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<CategoryFilterViewFactory>>();
            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<CategoryValueViewFactory>>();

            //viewTypeMapper.AddMappingStrategy<AssignmentMappingStrategy<IOpenFileDialogView>>();        //TODO: make IOpenFileDialogView derive from IView then this strategy won't be needed...
            //viewTypeMapper.AddMappingStrategy<ViewMappingStrategy>();

            //viewTypeMapper.AddMappings(viewAssembly.GetTypes());

            //set up form views/presenters/models
            //NOTE: remove these explicit mappings if using the dynamic assembly loading method to setup the views in the container...
            container.RegisterType<IEditUserMarkerView, EditUserMarkerView>();
            container.RegisterType<IFileBrowserView, FileBrowserView>();
            container.RegisterType<IOpenGenotypeFileView, OpenGenotypeFileView>();
            container.RegisterType<IOpenFileDialogView, SharedGenomics.Workbench.Views.WindowsForms.OpenFileDialog>();
            container.RegisterType<IOpenPhenotypeFileView, OpenPhenotypeFileView>();
            container.RegisterType<IFileSetUploadView, FileSetUploadView>();
            container.RegisterType<IDataFilterView, DataFilterView>();
            container.RegisterType<IAnalysisResultsView, AnalysisResultsView>();
            container.RegisterType<IXhtmlView, XhtmlView>();
            container.RegisterType<IWebBrowserView, SharedGenomics.Workbench.Views.WindowsForms.WebBrowserView>();
            container.RegisterType<IKeggPathwayFrequenciesView, KeggPathwayFrequenciesView>();
            container.RegisterType<IJobSubmissionView, JobSubmissionView>();
            container.RegisterType<IEmptyJobOptionsView, EmptyJobOptionsView>();
            container.RegisterType<IModelJobOptionsView, ModelJobOptionsView>();
            container.RegisterType<ICMHJobOptionsView, CMHJobOptionsView>();
            container.RegisterType<IAddSchemaView, SharedGenomics.Workbench.Views.WindowsForms.AddSchemaView>();
            container.RegisterType<IValidationErrorListView, SharedGenomics.Workbench.Views.WindowsForms.ValidationErrorListView>();
            container.RegisterType<IMainFormView, SharedGenomics.Workbench.Views.WindowsForms.MainFormView>();
            container.RegisterType<ICategoryFilterSetView, SharedGenomics.Workbench.Views.WindowsForms.CategoryFilterSetView>();
            container.RegisterType<IDataFilterView, SharedGenomics.Workbench.Views.WindowsForms.DataFilterView>();
            container.RegisterType<IDataFilterParametersView, SharedGenomics.Workbench.Views.WindowsForms.DataFilterParametersView>();
            container.RegisterType<IDataFilterResultsView, SharedGenomics.Workbench.Views.WindowsForms.DataFilterResultsView>();
            container.RegisterType<IWatchListView, SharedGenomics.Workbench.Views.WindowsForms.WatchListView>();
            container.RegisterType<IAnnotationWrapperView, SharedGenomics.Workbench.Views.WindowsForms.AnnotationWrapperView>();
            container.RegisterType<IAnalysisResultsFilterView, SharedGenomics.Workbench.Views.WindowsForms.AnalysisResultsFilterView>();
            container.RegisterType<IAnnotationOptionsView, SharedGenomics.Workbench.Views.WindowsForms.AnnotationOptionsView>();
            container.RegisterType<IAnnotationResultsView, AnnotationResultsView>();

            //annotation views
            container.RegisterType<IGeneSummaryView, SharedGenomics.Workbench.Views.WindowsForms.GeneSummaryView>();
            container.RegisterType<INcbiAnnotationSummaryView, SharedGenomics.Workbench.Views.WindowsForms.NcbiAnnotationSummaryView>();
            container.RegisterType<ITextMinedPublicationsView, SharedGenomics.Workbench.Views.WindowsForms.TextMinedPublicationView>();
        }
    }
}
